package it.polimi.swKnights.SWIMv2.ejb.modifyData;

import java.util.Date;

import it.polimi.swKnights.SWIMv2.ejb.authentication.LoginHandlerLocal;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.Address;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.RegisteredUser;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidConfirmationException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.utility.DateUtility;
import it.polimi.swKnights.SWIMv2.utility.PasswordUtility;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;



/**
 * Session Bean implementation class ModifyDataBean
 */
@Stateless
public class ModifyDataBean implements ModifyDataBeanRemote
{

	@PersistenceContext(unitName = "swimv2-manager")
	private EntityManager manager;

	@EJB
	private LoginHandlerLocal loginController;



	@Override
	public void modifyUserImage(String username, String password, String image)
			throws LoginFailException, InvalidLoginPrivilegesException
	{
		RegisteredUser user = this.getValidUser(username, password);

		String newImage = (image != null) ? image : user.getImage();

		user.setImage(newImage);

		this.manager.persist(user);
	}

	@Override
	public void modifyVisibility(String username, String password,
			boolean isPublic) throws LoginFailException,
			InvalidLoginPrivilegesException
	{
		RegisteredUser user = this.getValidUser(username, password);
		user.setPublic(isPublic);
		
		this.manager.persist(user);
	}

	@Override
	public void modifyPassword(String userName, String password,
			String newPassword, String confPassword, String oldPassword) throws LoginFailException,
			InvalidLoginPrivilegesException, InvalidConfirmationException
	{
		RegisteredUser user = this.getValidUser(userName, password);
		
		//new password & old passw can never be null, BUT here it's controlled for more safety
		if(oldPassword != null && newPassword != null && confPassword != null )
		{
			if(oldPassword.equals(password) && newPassword.equals(confPassword))
			{
				user.setPasswordHash(this.regeneratePassword(newPassword,
						user.geteMail()));
			} 
			else
			{
				throw new InvalidConfirmationException("Invalid passwords, old or new passwords error");
			}
				
		}
		else
		{
			throw new InvalidConfirmationException("Invalid passwords, check your old password");
		}
		
	}

	private String regeneratePassword(String password, String email)
	{
		PasswordUtility pUtility = new PasswordUtility();
		return pUtility.getEncriptedPassword(password, email);
	}

	private RegisteredUser getValidUser(String username, String password)
			throws LoginFailException, InvalidLoginPrivilegesException
	{
		// check for login
		this.loginController.checkLoginDataAndPrivileges(username, password,
				RegisteredUser.class);

		RegisteredUser user = manager.getReference(RegisteredUser.class,
				username);
		return user;
	}

	private char getSex(String sex)
	{
		if (sex.equalsIgnoreCase("M") || sex.equalsIgnoreCase("MALE"))
		{
			return 'M';
		}
		else if (sex.equalsIgnoreCase("F") || sex.equalsIgnoreCase("FEMALE"))
		{
			return 'F';
		}
		else
		{
			return 'N';
		}
	}

	@Override
	public void modifyUserData(String username, String password, String name,
			String surname, String email, String sex, String birthday, String address, 
			String passwordGiven, String passwordConfirm, String city,
			String street, String number) throws LoginFailException,
			InvalidLoginPrivilegesException {
	

		RegisteredUser user = this.getValidUser(username, password);

		char sexChar = (sex != null) ? this.getSex(sex) : user.getSex();
		String newMail = (email != null) ? email : user.geteMail();
		String newName = (name != null) ? name : user.getName();
		String newSurname = (surname != null) ? surname : user.getSurname();
		Date newBirthday = (birthday != null) ? DateUtility.getDate(birthday)
				: user.getBirthday();

		user.seteMail(newMail);
		user.setName(newName);
		user.setSurname(newSurname);
		user.setSex(sexChar);
		user.setBirthday(newBirthday);
		user.setPasswordHash(this.regeneratePassword(password, email));
		Address a = new Address();
		a.setCity(city);
		a.setNumber(number);
		a.setStreet(street);
		user.setAddress(a);
		

		this.manager.persist(user);

		
	}

}
